// pages/home/components/content/content.js
const http = require('../../../../http/app.js')
const map = require('../../../../http/qqmap-wx-jssdk.min.js')
var mapsdk;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    left: 0,
    scrollX: true,
    title: '',
    contentInfo: [],
    contentImg: [],
    imgUrl: 'https://www.sdhjfu.com/hftool/public/admin/', // 图片前半地址
    apartmentList: [],
    scale: 15,
    latitude: '',
    longitude: '',
    markers: [{
      id: 1,
      latitude: '',
      longitude: '',
      width: 25,
      height: 25,
      iconPath: '../../../../images/tag.jpg', // 替换默认地图标注
      //气泡label (可与callout 2选1)
      callout: {
        content: '2',  //文本
        color: '#555',  //文本颜色
        fontSize: 10,
        borderRadius: 3,  //边框圆角
        borderWidth: 0,  //边框宽度
        borderColor: '#fff',  //边框颜色
        bgColor: '#fff',  //背景色
        padding: 10,  //文本边缘留白
        display: "ALWAYS",
        textAlign: 'center'  //文本对齐方式。有效值: left, right, center
      }
    }]
  },

  // 方法
   // 点击banner
  bannerBut () {
    const result = []
    for (let i in this.data.contentImg) {
      result[i] = this.data.imgUrl + this.data.contentImg[i]
    }
    wx.previewImage({
      current: this.data.imgUrl + this.data.contentImg[0], // 当前显示图片的http链接
      urls: result // 需要预览的图片http链接列表
    })
  },
  // 点击户型列表
  apartmentBut(e) { 
    const ind = e.currentTarget.dataset.id;
    console.log(ind);
    const infoList = this.data.apartmentList;
    const current = this.data.imgUrl + infoList[ind].imgurl
    const result = [];
    for (let i in infoList) {
      result[i] = this.data.imgUrl + infoList[i].bigimgurl
    }
    wx.previewImage({
      current: current, // 当前显示图片的http链接
      urls: result // 需要预览的图片http链接列表
    })
  },
   //点击地图
  addressClick() {
    wx.openLocation({
      latitude: this.data.latitude,
      longitude: this.data.longitude,
      name: this.data.contentInfo.house_name,
      address: this.data.contentInfo.house_address
    })
  },
  // 拨打电话
  telBut () {
    wx.makePhoneCall({
      phoneNumber: '0531-8888656' //仅为示例，并非真实的电话号码
    })
  },
  // 点击查看更多楼盘信息
  moreInfo (e) {
    const info = JSON.stringify(e.currentTarget.dataset.item)//把数据变成字符串
    console.log(info)
    wx.navigateTo({
      url: '../moreContent/moreContent?info=' + info,
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const houseid = options.ind
    const that = this
    wx.showLoading({
      title: '数据加载中...',
      make: true
    })
    wx.request({
      url: http.housedetail,
      method: 'POST', //请求方式
      data: {
        houseid: houseid
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        // console.log(res)
        that.setData({
          contentInfo: res.data.houre,
          contentImg: res.data.imgmore,
          title: res.data.houre.house_name,
          apartmentList: res.data.apartre,
          markers: {
            callout: {
              content: res.data.houre.house_name
            }
          }
        })
        wx.hideLoading();
        wx.setNavigationBarTitle({
          title: that.data.title
        })
        // 获取地址名称并转经纬度
        let address = that.data.contentInfo.house_address
        mapsdk.geocoder({
          address: address, //要转化为经纬度的地址名称
          success: res => {
            //res里包含了该地址的信息
            let qqRes = res
            // console.log(qqRes)
            that.setData({
              latitude: qqRes.result.location.lat,
              longitude: qqRes.result.location.lng,
              markers: {
                latitude: qqRes.result.location.lat,
                longitude: qqRes.result.location.lng,
              }
            })
          }
        })
      }
    })
    // 实例化API核心类
    mapsdk = new map({
      key: '2KCBZ-XIEKU-W5TVA-2Y3FL-FABAT-J3FJZ'
    })
    this.getLocation()
  },
  getLocation() {
    console.log(333)
    
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    
  }
})